/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GalaxiaTrackerUI.java
 *
 * Created on 17 Apr, 2009, 6:29:11 PM
 */
package gtracker;

import java.sql.SQLException;
import java.util.*;
import java.io.*;

/**
 *
 * @author sv
 */
public class GalaxiaTrackerUI extends javax.swing.JFrame {

    public void setCurrentGID(int currentGID) {
        this.currentGID = currentGID;
    }

    public int getCurrentGID() {
        return currentGID;
    }

    public int getPushPort() {
        return Integer.parseInt(txtPushPort.getText());
    }

    public DynInfo getDynInfo() {
        return dynInfo;
    }

	/** Creates new form GalaxiaTrackerUI */
	public GalaxiaTrackerUI() {
		initComponents();
        setCurrentGID(0);
        try {
        dynInfo = new DynInfo();
        } catch(Exception e) {
            System.err.println(e);
        }
        gidMap = new Hashtable();
        trackerDB = new Database();

	}

    public void setStatus(String status) {
        lblStatus.setText("Status: " + status);
    }

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                jPanel1 = new javax.swing.JPanel();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                jLabel3 = new javax.swing.JLabel();
                txtDBUserName = new javax.swing.JTextField();
                txtDBName = new javax.swing.JTextField();
                txtDBPassWord = new javax.swing.JPasswordField();
                jPanel2 = new javax.swing.JPanel();
                jLabel5 = new javax.swing.JLabel();
                jLabel6 = new javax.swing.JLabel();
                jLabel7 = new javax.swing.JLabel();
                txtPassPhrase = new javax.swing.JPasswordField();
                txtTrackerPort = new javax.swing.JTextField();
                jSpinner1 = new javax.swing.JSpinner();
                btnChooseRandom = new javax.swing.JButton();
                jPanel3 = new javax.swing.JPanel();
                jButton5 = new javax.swing.JButton();
                jScrollPane1 = new javax.swing.JScrollPane();
                jTextArea1 = new javax.swing.JTextArea();
                jButton6 = new javax.swing.JButton();
                jButton7 = new javax.swing.JButton();
                jLabel8 = new javax.swing.JLabel();
                txtPushPort = new javax.swing.JTextField();
                lblStatus = new javax.swing.JLabel();
                btnStart = new javax.swing.JButton();
                btnStop = new javax.swing.JButton();
                jButton3 = new javax.swing.JButton();
                jButton8 = new javax.swing.JButton();

                setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                setTitle("Galaxia Tracker Interface");
                setResizable(false);

                jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Backend Database (mySQL) Options"));

                jLabel1.setText("UserName:");

                jLabel2.setText("Password:");

                jLabel3.setText("Database Name:");

                txtDBUserName.setText("root");
                txtDBUserName.setName("dbUName"); // NOI18N

                txtDBName.setText("galaxia");
                txtDBName.setName("dbName"); // NOI18N

        txtDBPassWord.setText("altraknite");
        txtDBPassWord.setName("dbPass"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDBUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDBPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDBName, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDBName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtDBUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(txtDBPassWord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

                jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Tracker Configuration"));

                jLabel5.setText("N (Trust Level):");

                jLabel6.setText("Port:");

                jLabel7.setText("PassPhrase:");

                txtPassPhrase.setText("public_tracker");
                txtPassPhrase.setName("dbPass"); // NOI18N

                txtTrackerPort.setText("6000");
                txtTrackerPort.setName("dbUName"); // NOI18N

                btnChooseRandom.setText("Choose Random");
                btnChooseRandom.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btnChooseRandomActionPerformed(evt);
                        }
                });

                jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Tracker Public Key"));

                jButton5.setText("Generate New >>");

                jTextArea1.setColumns(20);
                jTextArea1.setEditable(false);
                jTextArea1.setRows(5);
                jScrollPane1.setViewportView(jTextArea1);

                jButton6.setText("Import");

                jButton7.setText("Export");

                javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
                jPanel3.setLayout(jPanel3Layout);
                jPanel3Layout.setHorizontalGroup(
                        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                                .addGap(315, 315, 315)
                                                .addComponent(jButton7)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jButton6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE))
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 875, Short.MAX_VALUE)))
                                .addContainerGap())
                );
                jPanel3Layout.setVerticalGroup(
                        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addContainerGap(18, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jButton5)
                                        .addComponent(jButton6)
                                        .addComponent(jButton7))
                                .addContainerGap())
                );

                jLabel8.setText("PUSH Port");

                txtPushPort.setText("8000");
                txtPushPort.setName("dbUName"); // NOI18N

                javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
                jPanel2.setLayout(jPanel2Layout);
                jPanel2Layout.setHorizontalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel7)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtPassPhrase, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtTrackerPort, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(btnChooseRandom)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel8)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtPushPort, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addContainerGap())
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(1, 1, 1))))
                );
                jPanel2Layout.setVerticalGroup(
                        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel5)
                                        .addComponent(txtPassPhrase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel7)
                                        .addComponent(txtTrackerPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel6)
                                        .addComponent(btnChooseRandom)
                                        .addComponent(txtPushPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel8))
                                .addGap(18, 18, 18)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(25, Short.MAX_VALUE))
                );

                lblStatus.setText("Status: Ready");
                lblStatus.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

                btnStart.setText("Start >>");
                btnStart.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btnStartActionPerformed(evt);
                        }
                });

                btnStop.setText("Stop ||");
                btnStop.setEnabled(false);
                btnStop.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                btnStopActionPerformed(evt);
                        }
                });

                jButton3.setText("Preferences...");

                jButton8.setText("Snapshots");
                jButton8.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jPanel2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(lblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnStart)
                        .addGap(18, 18, 18)
                        .addComponent(btnStop)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 530, Short.MAX_VALUE)
                        .addComponent(jButton8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStart)
                    .addComponent(btnStop)
                    .addComponent(jButton3)
                    .addComponent(jButton8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(lblStatus)
                .addContainerGap())
        );

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
	       try {
            trackerDB.openConnection(txtDBUserName.getText(), txtDBPassWord.getText(),
                    "jdbc:mysql://localhost:3306/" + txtDBName.getText());

            new Dispatcher(Integer.parseInt(txtTrackerPort.getText()), this, trackerDB).start();

            File servConfigFile = new File("tracker.cfg");
            BufferedWriter configWriter = new BufferedWriter(new FileWriter(servConfigFile));
            configWriter.write(txtPassPhrase.getText(), 0, txtPassPhrase.getText().length());
            configWriter.close();

            setStatus("Connected to mySQL DB");
            btnStart.setEnabled(false);
            btnStop.setEnabled(true);
        } catch (Exception exp) {

            errMessage = exp.getCause().getMessage();
        }
}//GEN-LAST:event_btnStartActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed

	    trackerDB.closeConnection();

	    btnStop.setEnabled(false);
	    btnStart.setEnabled(true);
	    setStatus("Stopped");
    }//GEN-LAST:event_btnStopActionPerformed

    private void btnChooseRandomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseRandomActionPerformed

	    Random randGen = new Random();
	    int rand = randGen.nextInt();
	    if (rand < 0) {
		    rand = rand * -1;
	    }
	    txtTrackerPort.setText(Integer.toString(rand % 65536));
}//GEN-LAST:event_btnChooseRandomActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				// Set native OS look and feel
                try {
				javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
				}
				catch(Exception e) {
				System.out.println(e);
				}
				new GalaxiaTrackerUI().setVisible(true);
			}
		});
	}
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton btnChooseRandom;
        private javax.swing.JButton btnStart;
        private javax.swing.JButton btnStop;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton5;
        private javax.swing.JButton jButton6;
        private javax.swing.JButton jButton7;
        private javax.swing.JButton jButton8;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabel6;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel8;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JSpinner jSpinner1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JLabel lblStatus;
        private javax.swing.JTextField txtDBName;
        private javax.swing.JPasswordField txtDBPassWord;
        private javax.swing.JTextField txtDBUserName;
        private javax.swing.JPasswordField txtPassPhrase;
        private javax.swing.JTextField txtPushPort;
        private javax.swing.JTextField txtTrackerPort;
        // End of variables declaration//GEN-END:variables
	public Database trackerDB;
	//public Dispatcher dsp;
	protected String errMessage;
    public DynInfo dynInfo;
    protected int currentGID;
    public Hashtable<Integer, GroupInfo> gidMap;
    
	//protected boolean connectedToDB;
}
